//
//  THMajorDetailUserInfoViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/22.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THMajorDetailUserInfoViewController.h"

@interface THMajorDetailUserInfoViewController ()<STBWBrowserViewControllerDelegate>
@property(nonatomic, strong) NSMutableArray                     *imageViewArray;/**<  */
@end

@implementation THMajorDetailUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.contentInset = UIEdgeInsetsZero;
    UIScrollView * scrollView = [self.parentViewController valueForKey:@"scrollView"];
    self.tableView.frame = scrollView.bounds;
    [self stoneobs_ui_configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    UIView * header = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    header.backgroundColor = UIColor.whiteColor;
    
    CGFloat top = 0;
    
    ({
        STButton * button = [self viewFormLeftTitle:@"性别" value:self.model.cus_sex];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
    });
    
    ({
        STButton * button = [self viewFormLeftTitle:@"年龄" value:self.model.age];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
    });
    
    ({
        STButton * button = [self viewFormLeftTitle:@"职业年龄" value:self.model.job_age];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
    });
    
    ({
        STButton * button = [self viewFormLeftTitle:@"服务客户" value:[NSString stringWithFormat:@"%@人",self.model.sale_num]];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
    });
    
    
    
    ({
        STButton * button = [self viewFormLeftTitle:@"服务区域" value:self.model.service_area];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
    });
    
    
    ({
        STButton * button = [self viewFormLeftTitle:@"专业擅长" value:[self.model.majors componentsJoinedByString:@","]];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
    });
    
    ({
        STButton * button = [self viewFormLeftTitle:@"个人简介" value:self.model.brief];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
    });
    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, top, UISCREEN_WIDTH-30, 20)
                                                                text:@"资格证书"
                                                           textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                                font:13
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [header addSubview:stoneobs_ui_label];
        top = stoneobs_ui_label.bottom+ 10;
    });
    self.imageViewArray = NSMutableArray.new;
    __weak typeof(self) weakSelf =  self;
    UIView * contentView = [[UIView alloc] initWithFrame:CGRectMake(15, top, UISCREEN_WIDTH - 30, 105)];
    [header addSubview:contentView];

    CGFloat imageWitdh = (contentView.width - 3*10)/4;
    NSInteger min = MIN(self.model.imgs.count, 999999);
    for (NSInteger i = 0; i < min; i ++) {

        UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageWitdh, imageWitdh)];
        NSString * url = self.model.imgs[i];
        [stoneobs_ui_imageView sd_setImageWithURL:url.mj_url];
        [contentView addSubview:stoneobs_ui_imageView];
        stoneobs_ui_imageView.layer.cornerRadius = 4;
        stoneobs_ui_imageView.backgroundColor = WH_UICOLOR_FROM_RGBA(0xF6F6F6);
        stoneobs_ui_imageView.clipsToBounds = YES;
        stoneobs_ui_imageView.contentMode = UIViewContentModeScaleAspectFill;
        stoneobs_ui_imageView.userInteractionEnabled = YES;
        stoneobs_ui_imageView.left = imageWitdh * (i%4) + 10*(i%4);
        stoneobs_ui_imageView.top = stoneobs_ui_imageView.height * (i/4) + 10*(i/4);
        UITapGestureRecognizer * tap = [UITapGestureRecognizer.alloc initWithActionBlock:^(id  _Nonnull sender) {
            [weakSelf onSelctedButtonWithIndex:i];
        }];
        [stoneobs_ui_imageView addGestureRecognizer:tap];
        contentView.height = stoneobs_ui_imageView.bottom + 10;
        top = contentView.bottom;
        [self.imageViewArray addObject:stoneobs_ui_imageView];
    }
    
    header.height = top;
    self.tableView.tableHeaderView = header;
    
}
- (void)onSelctedButtonWithIndex:(NSInteger)index{
    //点击了某个图片
    NSMutableArray * models = [NSMutableArray new];
    for (NSString * url in self.model.imgs) {
        STBWPhotoModel * model = [STBWPhotoModel  new];
        model.originImageUrl = url;
        [models addObject:model];
    }
    STBWBrowserViewController * vc = [[STBWBrowserViewController alloc] initWithArray:models curentIndex:index];
    vc.shouldHideBottomView = YES;
    vc.delegate = self;
    vc.shouldHideTopView = YES;
    UIViewController * rootVC = TMUtils.stoneobs_fun_get_currentTopController;
    [rootVC presentViewController:vc animated:NO completion:nil];
}

#pragma mark --STBWBrowserViewControllerDelegate
- (UIView*)browserControllerDidScrollToIndexpath:(NSIndexPath *)indexPath model:(STBWPhotoModel *)model controller:(STBWBrowserViewController *)controller{

    return   self.imageViewArray[indexPath.row];
}
- (void)rightBarActionFromController:(STBWBrowserViewController *)controller currentIndexPath:(NSIndexPath *)curentIndexpath{


}
- (STButton*)viewFormLeftTitle:(NSString*)leftTitle value:(NSString*)value{
    STButton * view  = [[STButton alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, 40)
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:nil
                                       backgroundImage:nil
                                                 image:nil];
    
    
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, 120, view.height)
                                                            text:[NSString stringWithFormat:@"%@:",leftTitle]
                                                       textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                            font:13
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    [view addSubview:stoneobs_ui_label];
    stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:13];
    [NSString stoneobs_fun_lableAutoAdjustWitdhWithLabel:stoneobs_ui_label];
    stoneobs_ui_label.tag = 10002;
    
    STLabel * stoneobs_ui_s_label = [[STLabel alloc] initWithFrame:CGRectMake(stoneobs_ui_label.right, 0, STONEOBS_UI_UISCREEN_WIDTH -stoneobs_ui_label.right - 20 , view.height)
                                                              text:value
                                                         textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                              font:12
                                                       isSizetoFit:NO
                                                     textAlignment:NSTextAlignmentLeft];
    [view addSubview:stoneobs_ui_s_label];
    [NSString stoneobs_fun_lableAutoAdjustheightWithLabel:stoneobs_ui_s_label];
    if (stoneobs_ui_s_label.height < view.height) {
        stoneobs_ui_s_label.height = view.height;
    }else{
        view.height = stoneobs_ui_s_label.height + 20;
    }
    stoneobs_ui_label.centerY = view.height/2;
    stoneobs_ui_s_label.tag = 10001;
    UIView * stoneobs_ui_line = [[UIView alloc] initWithFrame:CGRectMake(15, view.height - 0.5, STONEOBS_UI_UISCREEN_WIDTH, 5)];
    stoneobs_ui_line.backgroundColor = self.tableView.backgroundColor;
        [view addSubview:stoneobs_ui_line];
    __weak typeof(self) weakSelf =  self;
    return view;
    
}
@end
